/*
 * This is the canvas of the program.
 */

package Test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JComponent;

public class MyCanvas extends JComponent implements MouseListener, MouseMotionListener{

	private GUI parent;
	private ArrayList<Point> allPoints;
	private ArrayList<Point> highlight;
	private Point press = null;
	private Point release = null;
	private Point drag = new Point(0, 0);
	
	public MyCanvas(GUI gui){
		parent = gui;
		this.setBackground(Color.BLACK);
		
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
		
	public void paintComponent(Graphics g){
		g.setColor(getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		
		g.setColor(Color.LIGHT_GRAY);
		for(Point p : allPoints){
			g.fillOval(p.x - 5, p.y - 5, 10, 10);
		}
		
		g.setColor(Color.GREEN);
		if(highlight != null)
			for(Point p : highlight){
				g.fillOval(p.x - 7, p.y - 7, 14, 14);
			}
		
		if(press != null && drag.x != 0){
			g.setColor(Color.YELLOW);
			g.drawRect(Math.min(press.x, drag.x), Math.min(press.y, drag.y), Math.abs(drag.x - press.x), Math.abs(drag.y - press.y));
		}
		else if(press != null && release != null && press.x != release.x && press.y != release.y){
			g.setColor(Color.YELLOW);
			g.drawRect(Math.min(press.x, release.x), Math.min(press.y, release.y), Math.abs(release.x - press.x), Math.abs(release.y - press.y));
		}
	}
	
	public void setAllPoints(ArrayList<Point> a){
		allPoints = a;
	}
	public void setHighLight(ArrayList<Point> a){
		highlight = a;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		press = new Point(e.getX(), e.getY());
		release = null;
		this.repaint();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		release = new Point(e.getX(), e.getY());
		
		long   start   =   System.currentTimeMillis();
		parent.query(press, release);
		long   end   =   System.currentTimeMillis();
		System.out.println("total time: " + (end - start));
		
		drag.setLocation(0, 0);
		this.repaint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		drag.setLocation(e.getX(), e.getY());
		
		parent.query(press, drag);
		
		this.repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
